<?php
/**
 * @title 2397. 被列覆盖的最多行数
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }


class Solution {

    /**
     * @param Integer[][] $matrix
     * @param Integer $numSelect
     * @return Integer
     */
    function maximumRows($matrix, $numSelect) {
        /**
         * @since 2024-01-04 行数和列数
         */
        $row = count($matrix);
        $col = count($matrix[0]);
        if($numSelect == $col){
            return $row;
        } else {}

        /**
         * @since 2024-01-04 把矩阵每一行1/0当一个二进制数，转成十进制数
         */
        $numArray = [];
        for($i=0; $i<$row; $i++){
            $numArray[$i] = bindec(implode("", $matrix[$i]));
        }

        /**
         * @since 2024-01-04 col列，二进制中有numSelect个0，进行AND位运算，结果为0的就是符合条件的
         * @since 2024-01-04 找出col位二进制中有numSelect个0的所有数集合
         */
        $selectArray = [];
        $nodeArray = [[str_repeat("1", $col), $numSelect]];
        for($i=0; $i<$col AND !empty($nodeArray); $i++){
            /**
             * @since 2024-01-04 下次遍历的集合
             */
            $nextNodeArray = [];
            // echo "\n\ni={$i}\n";

            /**
             * @since 2024-01-04 遍历本轮集合
             */
            foreach ($nodeArray as list($node, $n)){
                // echo "node: {$node}, n: {$n}". PHP_EOL;

                /**
                 * @since 2024-01-04 还有0可以选择
                 */
                if($n > 0) {
                    /**
                     * @since 2024-01-04 保持1不变，剩余的位数，要足够选择n个0
                     */
                    if($i+ $n < $col){
                        $nextNodeArray[] = [$node, $n];
                    } else {
                        /**
                         * @since 2024-01-04 已经不够选择0个0，放弃
                         */
                    }

                    /**
                     * @since 2024-01-04 选择0, numSelect-1
                     */
                    $node[$i] = "0";
                    $nextNodeArray[] = [$node, $n-1];
                } else {
                    /**
                     * @since 2024-01-04 没有0可以选择
                     */
                    $selectArray[] = bindec($node);
                    // echo "select: {$node}, ". bindec($node). PHP_EOL;
                }
            }

            /**
             * @since 2024-01-04 下次遍历
             */
            $nodeArray = $nextNodeArray;
        }

        /**
         * @since 2024-01-04 收官
         */
        foreach ($nodeArray as list($node, $n)){
            // echo "select: n={$n}, node={$node}, ". bindec($node). PHP_EOL;
            $selectArray[] = bindec($node);
        }
        // return [$selectArray, $numArray];

        /**
         * @since 2024-01-04 双循环，找出最大值
         */
        $maxCoveredCount = 0;
        foreach ($selectArray as $select){
            $coveredCount = 0;
            // echo "\n\nselect={$select}\n";

            /**
             * @since 2024-01-04 遍历所有行
             */
            foreach ($numArray as $num){
                // echo "num: {$num}, select: {$select}, &: ". ($num & $select). PHP_EOL;

                /**
                 * @since 2024-01-04 AND位运算，结果为0的就是覆盖该行
                 */
                if(($num & $select) == 0){
                    $coveredCount++;
                } else {}
            }

            /**
             * @since 2024-01-04 更新最大值
             */
            $maxCoveredCount = max($maxCoveredCount, $coveredCount);
        }

        /**
         * @return Integer
         */
        return $maxCoveredCount;
    }
}

/**
 * @url http://127.0.0.1/leetcode/202401/2024.01.04.php
 */
$datas = [
    [[[0,0,0],[1,0,1],[0,1,1],[0,0,1]], 2], // 3
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->maximumRows(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
